package com.huawei.crm.sample.adapter.inbound.rest.order;

import com.huawei.crm.sample.adapter.inbound.rest.order.converter.OrderReqConverter;
import com.huawei.crm.sample.adapter.inbound.rest.order.converter.OrderVOConverter;
import com.huawei.crm.sample.adapter.inbound.rest.order.request.OrderReq;
import com.huawei.crm.sample.adapter.inbound.rest.order.vo.OrderVO;
import com.huawei.crm.sample.application.order.OrderUseCase;
import com.huawei.crm.sample.application.order.dto.OrderDTO;
import com.huawei.crm.sample.application.order.gateway.CustomerGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    private final OrderUseCase orderUseCase;
    private final OrderReqConverter orderReqConverter;
    private final OrderVOConverter orderVOConverter;
    private final CustomerGateway customerGateway;

    @Autowired
    public OrderController(@Qualifier("orderUseCase") OrderUseCase orderUseCase, OrderReqConverter orderReqConverter, OrderVOConverter orderVOConverter, CustomerGateway customerGateway) {
        this.orderUseCase = orderUseCase;
        this.orderReqConverter = orderReqConverter;
        this.orderVOConverter = orderVOConverter;
        this.customerGateway = customerGateway;
    }

    @PostMapping
    public OrderVO placeOrder(@RequestBody OrderReq orderReq) {
        OrderDTO orderDTO = orderReqConverter.toDTO(orderReq);
        OrderDTO result = orderUseCase.placeOrder(orderDTO);
        return orderVOConverter.toVOWithCustomerName(result, customerGateway);
    }

    @GetMapping("/{orderId}")
    public OrderVO getOrder(@PathVariable String orderId) {
        OrderDTO result = orderUseCase.getOrder(orderId);
        return orderVOConverter.toVOWithCustomerName(result, customerGateway);
    }
} 